package cn.juque.lucenecandy.core.datasync;

import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.juque.lucenecandy.bo.IndexUpdateParamBO;
import cn.juque.lucenecandy.core.annotation.DataSync;
import cn.juque.lucenecandy.core.config.LuceneCandyConfig;
import cn.juque.lucenecandy.core.constants.StrConstant;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>DataSyncRender</li>
 * </ul>
 * @date 2024-01-02
 **/
@Component("dataSyncRender")
public class DataSyncRender {

    private static final Map<String, IDataSyncService> MAP = new HashMap<>();

    @Resource
    private LuceneCandyConfig luceneCandyConfig;

    public void init() {
        Map<String, IDataSyncService> map = SpringUtil.getBeansOfType(IDataSyncService.class);
        map.forEach((k, v) -> {
            String value = AnnotationUtil.getAnnotationValue(v.getClass(), DataSync.class, StrConstant.VALUE);
            MAP.put(value, v);
        });
    }

    public void commit(IndexUpdateParamBO param) {
        IDataSyncService dataSyncService = MAP.get(this.luceneCandyConfig.getDataSync());
        dataSyncService.commit(param);
    }

    public void cancel(IndexUpdateParamBO param) {
        IDataSyncService dataSyncService = MAP.get(this.luceneCandyConfig.getDataSync());
        dataSyncService.cancel(param);
    }
}
